Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

[1.x] Avoid deprecation warning about #basic_auth on Faraday::Connection #149

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from

Conversation

olleolleolle
Copy link

@olleolleolle olleolleolle commented Apr 29, 2022

This PR fixes a deprecation warning about Faraday.

Background

The #basic_auth helper method will be removed in 2.0, and this changes the code to use the middleware itself instead.

This implements the same thing as #147, but for librato-metrics 1.x.

The #basic_auth helper method will be removed in 2.0, and this changes the code to use the middleware itself instead.
@olleolleolle olleolleolle changed the title [1.x] Avoid Faraday deprecation warning [1.x] Avoid deprecation warning about #basic_auth on Faraday::Connection Apr 29, 2022
@olleolleolle
Copy link
Author

olleolleolle commented Apr 29, 2022

Workaround 🔧 in my Rails initializer: Mention the class, to have it autoload, then open it, and redefine the method.

# Avoid Deprecation warning from Faraday about the #basic_auth method
# being removed in Faraday 2.0.
#
# PR towards 1.x: https://github.com/librato/librato-metrics/pull/149
Librato::Metrics::Connection
module Librato
  module Metrics
    class Connection
      def transport
        raise(NoClientProvided, "No client provided.") unless @client

        @transport ||= Faraday::Connection.new(
          url: api_endpoint + "/v1/",
          request: { open_timeout: 20, timeout: 30 }) do |f|

          f.use Librato::Metrics::Middleware::RequestBody
          f.use Librato::Metrics::Middleware::Retry
          f.use Librato::Metrics::Middleware::CountRequests
          f.use Librato::Metrics::Middleware::ExpectsStatus

          f.adapter @adapter || Metrics.faraday_adapter
          f.proxy @proxy if @proxy
        end.tap do |transport|
          transport.headers[:user_agent] = user_agent
          transport.headers[:content_type] = "application/json"
          # transport.basic_auth @client.email, @client.api_key # This was the issue
          transport.request :basic_auth, @client.email, @client.api_key # This is the change
        end
      end
    end
  end
end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant